package com.up.cloudfeiyibackend.util;

/**
 * UserHolder工具类
 */
public class UserHolder {

    private static final ThreadLocal<Long> THREAD_LOCAL = new ThreadLocal<>();

    /**
     * 存储userId到当前线程上下文
     *
     * @param userId 登录用户ID
     */
    public static void set(Long userId) {
        THREAD_LOCAL.set(userId);
    }

    /**
     * 从当前线程上下文获取userId
     *
     * @return userId
     */
    @SuppressWarnings("unchecked")
    public static Long get() {
        return THREAD_LOCAL.get();
    }

    /**
     * 清除当前线程上下文，防止内存泄漏
     */
    public static void remove() {
        THREAD_LOCAL.remove();
    }
}
